package com.hup.joystick.model.joystick;

import com.hup.joystick.constant.DeviceKey.KeyActionType;
import com.hup.joystick.service.jna.DIJoyState2;
import com.hup.joystick.service.jna.DInputHResultException.HResult;
import com.hup.joystick.service.jna.DeviceStateCallback;

import java.util.ArrayList;

/**
 * @author hugan
 * @date 2020/10/20
 */
public class NativeEvent {

    /**
     * 设备状态的原始数据
     */
    public final DIJoyState2 state;
    /**
     * 状态查询结果; {@link HResult}
     */
    public final int hr;
    /**
     * {@link DeviceStateCallback#onDeviceState(DIJoyState2, int)}中产生的程序异常
     */
    public Throwable e;
    /**
     * 当前按键事件:[新按下的,新抬起的]
     */
    public final ArrayList<NativeAction> actions = new ArrayList<>();
    /**
     * 当前被按下的按钮事件
     */
    public final ArrayList<NativeAction> currentPress = new ArrayList<>();

    public NativeEvent(DIJoyState2 state, int hr) {
        this.state = state;
        this.hr = hr;
    }

    @Override
    public String toString() {
        return "JoyActionEvent{" +
                "hr=" + hr +
                ", actions=" + actions +
                '}';
    }

    public static class NativeAction {
        public final NativeKey nativeKey;
        /**
         * {@link KeyActionType}
         */
        public int actionType;

        public NativeAction(NativeKey nativeKey, int actionType) {
            this.nativeKey = nativeKey;
            this.actionType = actionType;
        }

        @Override
        public String toString() {
            return "[" +
                    "key='" + nativeKey.key + '\'' +
                    ", value=" + nativeKey.nativeValue +
                    ", type=" + actionType +
                    ']';
        }
    }

}
